# coding=utf-8

from pymongo import MongoClient
from bs4 import BeautifulSoup
import requests
import datetime
import pprint
import json
import time
import re

server_client = MongoClient('47.104.130.19', 27017)

server_db = server_client['knx_posts_db']
offical_posts_coll = server_db['offical_posts_coll']

class POST():
    def __init__(self):
        self.company = '招商证券股份有限公司'
        self.url = "http://www.hotjob.cn/wt/cms/web/index/webPosition210!getPostListByConditionShowPic"
        self.params = {
            'columnId':'1',
            'operational':'ef41d3c20fef4455c7a9729b2b2c2c2883025e911b2b4dd78b9fc6888a128d70163addcb2774d27f8e96dd115a844e9b40e502283d8325a251604a5a87163136c1e52b3d0f558ba957bc5fe99522ce7122d8b2e147b111e102166381363ae4e1'
        }
        self.data = {
            'pc.currentPage': 2,
            'pc.rowSize': 10,
            'orgId':'',
            'releaseTime':'',
            'keyWord':'',
            'positionType':'',
            'trademark': 1,
            'workPlace':'',
            'useForm':'',
            'positionName':'',
            'recruitType':1,
            'specialRecruitmentId':'',
            'brandCode':1,
            'searchType': 1,
            'blockType': 1,
            'contentModuleType':'',
            'tagType':'',
            'comPart':'',
            'orderInt':0,
            'workPlaceNum': 5,
            'workTypeNum': 5,
            'workCompanyNum': 5,
            'colPoitionName': 'a',
            'colPositionType':'',
            'colPositionCompany':'c',
            'colPostionRecruit': 'd',
            'colPositionWorkPlace': 'e',
            'colPostionReleaseTime':'',
            'positionNameLength':200,
            'positionTypeLength':'',
            'postionCompanyLength':170,
            'positionRecruitLength':'',
            'positionWorkPlaceLength':180,
            'postionReleaseTimeLength':'',
            'positionColUseDefault':1,
            'positionNameV':'',
            'workPlaceNameV':'',
            'positionTypeV':'',
            'sort_order':'by_time'
        }

        self.scrapy()

    def scrapy(self):
        for i in range(1,3):
            self.data['pc.currentPage'] = i
            r = requests.post(self.url, params = self.params, data = self.data)
            soup = BeautifulSoup(r.text)
            for a in soup.find(class_ = 'search_result').find_all('a'):
                url = 'http://www.hotjob.cn' + a['href']
                r = requests.get(url)
                soup = BeautifulSoup(r.text, 'html5lib')
                description = soup.find(class_ = 'position_content_title').get_text() \
                              + soup.find(class_ = 'position_content_title').find_next_sibling().get_text() \
                              + soup.find(class_ = 'position_content_title2').get_text() \
                              + soup.find(class_ = 'position_content_title2').find_next_sibling().get_text()

                name = soup.find(class_ = 'position_title').get_text().strip()
                location = soup.find_all(class_ = 'position_basic_title')[3].find_next_sibling().get_text()

                item = {
                    "url": url, #jd详情页的地址
                    'edu': soup.find_all(class_ = 'position_basic_title')[5].next_sibling()[0].get_text(), # 最低学历
                    'exp': [], # 所需工作经验，比如[3, 5]表示3到5年, [3]表示3年，[]表示无经验要求
                    'name': name , # 职位名称 *
                    'date': soup.find_all(class_ = 'position_basic_title')[7].find_next_sibling().get_text(), # 职位发布日期，字符串形式即可，后期统一转换
                    'lang': '', # 对语言的要求
                    'place': '', # 办公具体地址
                    'major': '', # 专业要求
                    'count': soup.find_all(class_ = 'position_basic_title')[4].find_next_sibling().get_text(), # 招聘数量
                    'salary': [], # 薪资待遇，[5000, 8000]表示月薪5到8千，[4000]表示4千，[]表示没有写明
                    'toSchool': True, # 是否是面向校园招聘，本次官网抓取一律都是校园招聘，所以此处都是True
                    'welfare': [], # 福利待遇，比如五险一金、十三薪之类的，保存成数组
                    'funType': '', # 职能类型，比如证券经纪人是证券 / 期货 / 外汇经纪人
                    'company': self.company, # 企业名称
                    'location': location, # 所在城市
                    'industry': '金融行业', # 企业所在行业
                    'keywords': [], # 此岗位的搜索关键字
                    'platform': 'offical', # 针对官网抓取时此处一律保存为offical
                    'searchKeyword': '', # 搜索的关键字，由于是官网抓取所以此处一律为空字符串
                    'description': description, # 职位的详细描述，包括职责、要求之类的
                    'subIndustry': '', # 一律为空字符串
                    'stime': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 抓取时间
                }

                # if offical_posts_coll.insert(item):
                #     print('insert one job')
                # pprint.pprint(item)

POST()
